package com.android.handset;

import static com.android.handset.ConsumerIrManagerApi.hasIrEmitter;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

	private BluetoothController mBluetoothController;
	public static final int REQUESTCODE_OPEN = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mBluetoothController = new BluetoothController();
		ConsumerIrManagerApi.getConsumerIrManager(MainActivity.this);

		findViewById(R.id.bt1).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d("TAG", "onClick: bt1");
				Toast.makeText(MainActivity.this,"发送按键---蓝牙",Toast.LENGTH_SHORT).show();
			}
		});

		findViewById(R.id.bt2).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d("TAG", "onClick: bt2");
				Toast.makeText(MainActivity.this,"发送按键---WIFI",Toast.LENGTH_SHORT).show();
				new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							Socket socket_client =new Socket("172.20.2.83",54321);
							OutputStream os = socket_client.getOutputStream();
							DataOutputStream dos = new DataOutputStream(os);
							dos.writeUTF("abc\r\n123\r\n5678\r\n");

						} catch (Exception e) {
							Log.i("socket", e.getMessage());
							//e.printStackTrace();
						}
					}
				}).start();
			}
		});

		findViewById(R.id.bt3).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Log.d("TAG", "onClick: bt3");
				Toast.makeText(MainActivity.this,"发送按键---红外 --- 是否支持红外【"  + hasIrEmitter() + "】",Toast.LENGTH_SHORT).show();
				transmit();
			}
		});

	}



	/**
	 * 按下按键发射红外信号
	 */
	public void transmit() {
		ConsumerIrManagerApi.transmit(38000, (NecPattern.buildPattern(0X0e,0X0e, 0X44)));
	}
}